package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultXzq;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.factory.AdjustModelFactory;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultXzqMapper;
import com.geostar.constructionland.analyse.service.BaseAdjustService;
import com.geostar.constructionland.analyse.service.analyseresult.AlyResultXzqService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author yelll
 * @since 2020-5-27 15:56
 **/
@Component
@Slf4j
public class XzqHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    AlyResultXzqMapper alyResultXzqMapper;
    @Autowired
    BaseAdjustService adjustService;
    @Autowired
    AlyResultXzqService alyResultXzqService;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理行政区占压返回数据,状态码:{}",httpResult.getCode());
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            result.setMessage(alyResult.getUniqueTaskId());
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultXzq> alyResultXzqs = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultXzq>>(){});
                if(alyResultXzqs.size()>0){
                    alyResultXzqs.forEach(xzq -> {
                        xzq.setAlyId(UUID.randomUUID().toString().replaceAll("-",""));
                    });
                    String newResult = objectMapper.writeValueAsString(alyResultXzqs);
                    alyResult.getAnalysisResult().setResultRecords(newResult);

                    /**执行平差**/
                    AdjustModel adjustModel = AdjustModelFactory.getAdjustMod(analyseModel,alyResult);
                    /**平差后的结果**/
                    List<LandInfo> landInfo_adjusted = adjustService.doAjust(analyseModel,adjustModel);
                    /**设置分析标示**/
                    for(AlyResultXzq alyResultXzq : alyResultXzqs){
                        alyResultXzq.setAlyCode(alyResult.getAnalyseId());
                        /**更新占压面积**/
                        List<LandInfo> filterLandInfos = landInfo_adjusted.stream().
                                filter(landInfo -> (landInfo.getDkid().equals(alyResultXzq.getLandCode()))).collect(Collectors.toList());
                        if(filterLandInfos.size()>0)
                        {
                            alyResultXzq.setOcyAreaBefore(filterLandInfos.get(0).getZymj());
                            alyResultXzq.setOcyArea(filterLandInfos.get(0).getZymj_adjusted());
                        }
                    }
                }
                /**批量保存**/
                alyResultXzqService.saveBatchXzqGeoJson(alyResultXzqs);
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
